using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace InsertVideo
{
    public partial class ContentEditor : SmartContentEditor
    {
        AssemblyInfo ai = new AssemblyInfo();
        VideoSettings m_settings;
        ISmartContent m_content;

        public ContentEditor()
        {
            
            InitializeComponent();
            lblCopyright.Text = ai.AssemblyCopyright;
            lblPluginName.Text = string.Format("{0} v{1}", ai.AssemblyProduct, ai.AssemblyVersion);
            this.SelectedContentChanged += new EventHandler(ContentEditor_SelectedContentChanged);
        }

        void ContentEditor_SelectedContentChanged(object sender, EventArgs e)
        {
            m_content = SelectedContent;
            m_settings = new VideoSettings(m_content.Properties);
            ApplySettings();
        }

        private void ApplySettings()
        {
            chOpenNewWindow.Checked = m_settings.OpenNew;
            cbOptions.SelectedIndex = m_settings.Option;
            cbOptions_SelectedIndexChanged(this, null);
            chTransparancy.Checked = m_settings.Transparency;
            chTitle.Checked = m_settings.TitleIsEnabled;
            chAutoplay.Checked = m_settings.Autoplay;            
            chAutoplay.Enabled = m_settings.AllowAutoPlay;
            cbMargins.SelectedIndex = m_settings.MarginState;
            chYoutube.Checked = m_settings.YouTubeRelative;
            if (m_settings.Provider == "youtube")
                chYoutube.Visible = true;
            else
                chYoutube.Visible = false;
            
            if (m_settings.Provider == "muveemix")
            {
                lklblTitle.Text = "MuveeMix Video";
                chTitle.Enabled = false;
            }
            else
            {
                lklblTitle.Text = SetTitle(m_settings.Title);
                chTitle.Enabled = true;
            }
            
        }

        private string SetTitle(string origTitle)
        {
            if (origTitle.Length > 30)
                return origTitle.Substring(0, 30) + "...";
            else
                return origTitle;

        }

        private void cbOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbOptions.SelectedIndex)
            {
                case 0:
                    chOpenNewWindow.Enabled = false;
                    chAutoplay.Enabled = true;                    
                    chTransparancy.Enabled = true;
                    chTitle.Enabled = true;
                    chYoutube.Enabled = true;
                    break;
                case 1:
                    chOpenNewWindow.Enabled = true;
                    chAutoplay.Enabled = false;
                    chTransparancy.Enabled = false;
                    chTitle.Enabled = true;
                    chYoutube.Enabled = false;
                    break;
                case 2:
                    chOpenNewWindow.Enabled = true;
                    chAutoplay.Enabled = true;
                    chTransparancy.Enabled = true;
                    chTitle.Enabled = true;
                    chYoutube.Enabled = true;
                    break;
            }
            m_settings.Option = cbOptions.SelectedIndex;
            OnContentEdited();
        }

        private void chOpenNewWindow_Click(object sender, EventArgs e)
        {
            m_settings.OpenNew = chOpenNewWindow.Checked;
            OnContentEdited();
        }

        private void linkFeedback_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = "mailto:wlm@scottisafool.co.uk?Subject=Feedback from Video Plugin";
            process.Start();

        }

        private void linkBlog_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = "http://scottisafooldev.spaces.live.com/";
            process.Start();
        }

        private void chTransparancy_Click(object sender, EventArgs e)
        {
            m_settings.Transparency = chTransparancy.Checked;
            OnContentEdited();
        }

        private void lklblTitle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = m_settings.Link;
            process.Start();
        }

        private void chTitle_Click(object sender, EventArgs e)
        {
            m_settings.TitleIsEnabled = chTitle.Checked;
            OnContentEdited();
        }

        #region Debug Button
        private void button1_Click(object sender, EventArgs e)
        {
            string debug = "Default Width: " + m_settings.DefaultWidth.ToString() +
                "\nIs Enabled: " + m_settings.IsEnabled.ToString() +
                "\nDefault Option: " + m_settings.DefaultOption.ToString() +
                "\nDefault Open: " + m_settings.DefaultOpen.ToString() +
                "\nTransparency: " + m_settings.DefaultTransparancySetting.ToString() +
                "\nDefault Title: " + m_settings.DefaultTitleIsEnabled +
                "\nPreview: " + m_settings.Preview.ToString() +
                "\nProvider: " + m_settings.Provider +
                "\nID: " + m_settings.Id +
                "\nWidth: " + m_settings.Width.ToString() +
                "\nEmbed: " + m_settings.Embed.Substring(0, 50) +
                "\nLink: " + m_settings.Link +
                "\nOption: " + m_settings.Option.ToString() +
                "\nRatio: " + m_settings.Ratio.ToString() +
                "\nTitle: " + m_settings.Title +
                "\nOpen New: " + m_settings.OpenNew.ToString() +
                "\nTransparency: " + m_settings.Transparency.ToString() +
                "\nTitle Enabled: " + m_settings.TitleIsEnabled.ToString() +
                "\nFinal Embed: " + m_settings.FinalEmbedCode.Substring(0, 50);

            MessageBox.Show(debug);
        }
        #endregion

        private void chAutoplay_CheckedChanged(object sender, EventArgs e)
        {
            m_settings.Autoplay = chAutoplay.Checked;
            OnContentEdited();
        }

        private void cbMargins_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbMargins.SelectedIndex)
            {
                case 0:
                    grpMargins.Visible = false;
                    m_settings.Top = 0;
                    m_settings.Left = 0;
                    m_settings.Bottom = 0;
                    m_settings.Right = 0;
                    OnContentEdited();
                    break;
                case 1:
                    grpMargins.Visible = true;
                    ChangeMargin(numericUpDown1, 1);
                    ChangeMargin(numericUpDown2, 2);
                    ChangeMargin(numericUpDown3, 3);
                    ChangeMargin(numericUpDown4, 4);
                    OnContentEdited();
                    break;
            }
            m_settings.MarginState = cbMargins.SelectedIndex;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            ChangeMargin(numericUpDown1, 1);
            OnContentEdited();
        }

        private void ChangeMargin(NumericUpDown nup, int p)
        {
            switch (p)
            {
                case 1:
                    m_settings.Top = (int)nup.Value;
                    break;
                case 2:
                    m_settings.Right = (int)nup.Value;
                    break;
                case 3:
                    m_settings.Bottom = (int)nup.Value;
                    break;
                case 4:
                    m_settings.Left = (int)nup.Value;
                    break;
            }

        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            ChangeMargin(numericUpDown2, 2);
            OnContentEdited();
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            ChangeMargin(numericUpDown3, 3);
            OnContentEdited();
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            ChangeMargin(numericUpDown4, 4);
            OnContentEdited();
        }

        private void chYoutube_CheckedChanged(object sender, EventArgs e)
        {
            m_settings.YouTubeRelative = chYoutube.Checked;
            OnContentEdited();
        }
    }
}
